package sanrenxing.login.servlet;

import sanrenxing.login.dao.LoginDao;
import sanrenxing.login.dao.impl.LoginDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Map;
import java.util.List;

/**
 * @author Administrator
 * @date 2020/10/13 22:15
 * 编辑者：邓志强
 * 功能：
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    LoginDao loginDao=new LoginDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求字符集
        req.setCharacterEncoding("utf-8");
        //设置响应字符集和内容类型
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        //接收来自客户端方法信息
        String method=req.getParameter("method");
        if (method.equals("studentLogin")){
            this.studentLogin(req,resp);
        }else if (method.equals("teacherLogin")){
            this.teacherLogin(req,resp);
        }

    }

    /**
     * 教师登录验证
     * @param req
     * @param resp
     */
    private void teacherLogin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //创建输出流
        PrintWriter out=resp.getWriter();
        //接收来自客户端的账号密码
        String userName=req.getParameter("userName");
        String password=req.getParameter("password");
        //通过教师账号查询教师编号
        int teacherId=loginDao.findTeacherId(userName);

        //调用数据层代码，验证教师登录,返回roleId角色编号和角色对应功能名称字符串组
        Map<String,Object> map=loginDao.teacherVerify(userName,password);
        int roleId=(Integer) map.get("roleId");
        String funId=(String)map.get("funId");
        if (roleId!=0){
            String[] funIds=funId.split(",");
            //创建list集合用来保存每一个功能编号
            List<String> funIdlist=new ArrayList<>();
            for (String str:funIds) {
                funIdlist.add(str);
            }
            //添加session
            HttpSession session=req.getSession();
            //将功能编号字符串添加到session当中
            session.setAttribute("funIdlist",funIdlist);
            session.setAttribute("roleId",roleId);
            session.setAttribute("userName",userName);
            session.setAttribute("teacherId",teacherId);
            out.print("ok");
        }else{
            out.print("no");
        }
    }

    /**
     * 学员登录验证
     * @param req
     * @param resp
     */
    private void studentLogin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //创建输出流
        PrintWriter out=resp.getWriter();

        //接收来自客户端的账号密码
        String userName=req.getParameter("userName");
        String password=req.getParameter("password");
        //调用验证方法
        String stuId=loginDao.verify(userName,password);
        //创建json对象
        //JsonObject jsonObject=new JsonObject();
        if (stuId!=null){
            /*jsonObject.addProperty("state","ok");
            jsonObject.addProperty("stuId",stuId);
            out.print(jsonObject);*/
            //创建session并给sesion赋值stuId,该stuId用于证明登录的学员身份
            HttpSession session=req.getSession();
            //session.getId()
            session.setAttribute("stuId",stuId);
            out.print("ok");

        }else{
            /*jsonObject.addProperty("state","no");
            jsonObject.addProperty("stuId","");
            out.print(jsonObject);*/
            out.print("no");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

}
